/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * 
 * Copyright 2010 yura, Version 2.0 (the "License");
 *
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 
 * Unless required by applicable law or agreed to in writing, software
 *
 * distributed under the License is distributed on an "AS IS" BASIS,
 *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */
package org.opu.doc2db.document.datadocument;

import java.util.Collection;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
 *  final paragraph in table - cell, thet consees width
 * and texts
 * 
 * @author yura
 */
@Root(name = "td")
public class TableCell extends ParagraphList<TextParagraph> {

    /** Cell width */
    @Element(name = "width", required = false)
    private Metric<Integer> width;
    /** Is current cell is veisble */
    @Element(name = "visible")
    private boolean visible;
    /** Number of cell that current cell is combines */
    @Element(required=false)
    private int hsplit = 1;

    public TableCell(String id,
            Collection<TextParagraph> paragraph,
            Metric<Integer> width,
            boolean visible) {
        super(id, paragraph);
        this.width = width;
        this.visible = visible;
        super.dataSeparator = " \u00b6 ";//" ¶ "
    }

    /**
     *
     * @param id id of paragraph
     * @param paragraph text
     * @param width width
     */
    public TableCell(String id,
            Collection<TextParagraph> paragraph,
            Metric<Integer> width) {
        this(id, paragraph, width, true);
    }

    /**
     *
     * @param id id of paragraph
     * @param paragraph text
     */
    public TableCell(String id,
            Collection<TextParagraph> paragraph) {
        this(id, paragraph, Metric.emptyMetric());
    }

    /**
     *
     * @param paragraph text
     * @param width width
     */
    public TableCell(
            Collection<TextParagraph> paragraph,
            Metric<Integer> width) {
        this("", paragraph, width);
    }

    /**
     * 
     * @param paragraph text
     */
    public TableCell(
            Collection<TextParagraph> paragraph) {
        this(paragraph, Metric.emptyMetric());
    }

    public TableCell() {
        this(null);
    }

    /**
     *  Returns an array containing all of the elements
     * in paragraphs (texts) list
     *
     * @return array containing all of the cells
     */
    public TextParagraph[] cellsToArray() {
        Object[] obj = elements.toArray();
        TextParagraph[] ret = new TextParagraph[obj.length];

        System.arraycopy(obj, 0, ret, 0, obj.length);

        return ret;
    }

    /**
     * Get the value of width
     *
     * @return the value of width
     */
    public Metric<Integer> getWidth() {
        return width;
    }

    /**
     * Set the value of width
     *
     * @param width new value of width
     */
    public void setWidth(Metric<Integer> width) {
        this.width = width;
    }

    public void setWidthMetric(Integer metricName) {
        width.setMetric(metricName);
    }

    public Integer getWidthMetric() {
        return width.getMetric();
    }

    public void setWidthMetricName(String metricName) {
        width.setName(metricName);
    }

    public String getWidthMetricName() {
        return width.getName();
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public int getHsplit() {
        return hsplit;
    }

    public void setHsplit(int hsplit) {
        this.hsplit = hsplit;
    }
}
